<?php
namespace db;

/**
 *
 * @author Jakub Dykowski
 *        
 */
class MysqlQueryResult extends AbstractQueryResult {
	private $result;
	private $affectedRows;
	private $insertId;

	function __construct(\mysqli_result $result,\mysqli $connection) {
		$this->result = $result;
		$this->affectedRows = $connection->affected_rows;
		$this->insertId = $connection->insert_id;
	}

	protected function fetch() {
		while ($row = $this->result->fetch_array(MYSQLI_ASSOC)) {
			self::addRow($row);
		}
		$this->result->free();
	}

	function count() {
		// TODO affected_rows sometime is -1 (when print_r is calld on $this before ensureFetch is called, > -1 fixes it, but thats is wierd)
		return $this->affectedRows > -1 ? $this->affectedRows : $this->result->num_rows;
	}

	function id() {
		// if ($this->conn->insert_id == 0)
		// throw new Exception("it is not an insert query result, or maybe the field is not autoincrement and id wasn't specified");
		return $this->conn->insert_id;
	}
}